<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<% 
String idGry=request.getParameter("game_id");
int game_id = Integer.parseInt(idGry);
%>
    <%@ page import="servlets.*"%>
<%@ page import="org.hibernate.SessionFactory"%>
<%@ page import="org.hibernate.Session"%>
<%@ page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@ page import="hibernate.model.*"%>
<%@ page import="hibernate.util.HibernateUtil"%>
<%@ page import="hibernate.method.*"%>
<script>
$("a.editGame").click(function() {
		$("#games_content").hide().fadeIn(1000).load("games/editGame.jsp?game_id="+ $(this).attr('id'));
		return false;
	});
</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<%
						SessionFactory sessionFactory = HibernateUtil
								.getSessionAnnotationFactory();
						Session s = sessionFactory.openSession();
						s.beginTransaction();
						UserMethods um= new UserMethods();
						
						GameMethods gm = new GameMethods();
							response.setContentType("text/html");
							Game g = gm.idToGame(sessionFactory, s, game_id);
							FeedbackMethods fb = new FeedbackMethods();
							List<GameFeedback> listfb = fb.listGameFeedbacks(sessionFactory, s, game_id);
							int commentsCount = fb.countFbByGameId(sessionFactory, s, game_id);
							Integer user = (Integer) session.getAttribute("user_id");
					%> 
				
<div class="page-header">
	<h2 class="page-title">Nasze gry  -  <%=g.getName() %></h2>
</div>
<div id="content">
<br />

			<table>

<tr>
<td>
		<div class="col-md-4 col-sm-6" style="width: 666px;">
											<div class="team-member">
												 <div class="member-thumb">
												<!--	<img src="images/team3.jpg" alt=""> -->
												</div>
												<div class="member-infos">
													<h4 class="member-name">
													<%out.println("<a class=\"games\" id=\""+ game_id + "\" href=\"#\">" + g.getName() + "</a>"); %>
													<%out.println("<a class=\"editGame\" id=\""+ game_id + "\" href=\"#\">" + "EDYTUJ GRE" + "</a>"); %></h4>
													<span class="member-role">
													<%out.println("Ogólna ocena uzytkownikow:"+ "  "); %>
													<br />
													<%out.println("Komentarzy: "+ commentsCount); %>
													</span>
													
													<% String opis1 = null;
													if (g.getDescribe()== null || g.getDescribe().equals("0")) opis1 = "Brak";
													else opis1 = g.getDescribe();%>
													<p class="member-desc"><%out.println("Opis gry: "+ opis1);%></p>
													<br>
														<%
													GameCategoryMethods gcat = new GameCategoryMethods();
													List<String> gamecategories = new ArrayList<String>();
													gamecategories = gcat.listCategoriesByGame(sessionFactory, s, game_id);
													if (gamecategories == null) out.println("Gra nie ma jeszcze zadnej kategorii");
													else if (gamecategories.size() >0) {out.println("Kategorie: ");
													for (String gc : gamecategories){
														out.println(gc+", ");
													}
													}
													%>
													</div>
													</div>
	
													</td>
													</tr></table>
													<form role="form" id='addGameRate'action="../addGameRate" method="POST">
													Ocena: <select id="rate" name="rate">
													<option value="0">0</option>
													<option value="1">1</option>
													<option value="2">2</option>
													<option value="3">3</option>
													<option value="4">4</option>
													<option value="5">5</option>
													</select>
														<input type="submit" class="mainBtn" value="Oceń" />	
													</form>
													<br>
													<div class="page-header">
	<h2 class="page-title" style="color: white;">Komentarze (<%=commentsCount %>)</h2>
</div>

																										
													
		<form role="form" id='editGame'action="../addGameFeedback" method="POST">
		<p class="member-desc" style="color: white;">
		<input type="hidden" name="game_id" id="game_id" value="<%=game_id%>" />
		Jako: <input type="hidden" id="user" name="user" value="<%=user%>" />
		<% if (user != null){
		User u = um.id_to_user(sessionFactory, s, user);
		out.print(u.getUsername());
		 }else {
														out.print("Anonim");
														} %>
			<br>											
		<textarea name="comment" rows="3" class="form-control" id="comment"  placeholder="Tresc komentarza"></textarea>
				<input type="submit" class="mainBtn" value="Dodaj komentarz" />	
			</p>
		</form>
		<p class="member-desc" style="color:white;">
													<%
													if (listfb.size()> 0){
														for (GameFeedback fb1 : listfb)
														{%>
														
														<%	String userName = null;
															if(!fb1.getUser_id().equals("null")){
															String user_id = fb1.getUser_id();
															User u = um.id_to_user(sessionFactory, s, Integer.parseInt(user_id));
															 userName = u.getUsername();
															} else userName = "Anonim";
															String comment = fb1.getComment();
															String add_date = fb1.getAdd_date();
															%>
															<table>
																<form role="form" id='deleteFb'action="../deleteGameFeedback" method="POST">
															<input type="hidden" id="comment_id" name="comment_id" value="<%=fb1.getId()%>" />
<input type="submit" class="mainBtn" value="Usuń" />
</form>
															<tr>
															<td>
															Uzytkownik:
															</td>
															<td><%
															out.println(userName);
															%>
															</td>
															</tr>
															<tr>
															<td>
															Data dodania:
															</td>
															<td>
															<%out.println(add_date); %>
															</td></tr>
															<tr><td>
															Tresc komentarza:
															</td>
															<td><%
															out.println(comment);
															%>
															</td>
															</tr>
															</table><%
															out.println("<br />");
														}
														
													}
													else if (listfb.size() == 0) out.println("Ta gra nie ma jeszcze zadnych komentarzy");
													%>
													</p>
												</div>
												
										</div>
										
									
								
	<%	s.getTransaction().commit();	%>

</div>		
		